<?php

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/

/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(
    App\User::class,
    function (Faker\Generator $faker) {
        static $password;

        return [
            'name'                 => $faker->name,
            'phone'                => $faker->phoneNumber,
            'hospital_id'          => $faker->numberBetween(1, 30),
            'department'           => 1,
            'password'             => $password ?: $password = bcrypt('secret'),
            'remember_token'       => str_random(10),
            'province'             => 0,
            'city'                 => 0,
            'county'               => 0,
            'nation'               => 0,
            'birthday'             => $faker->date(),
            'show_Mobile'          => $faker->boolean(90) ? 1 : 0,
            'invite_code'          => $faker->shuffleString('asdfghjklpoiuytrewqzxcvbnm'),
            'invite_original_code' => $faker->shuffleString('asdfghjklpoiuytrewqzxcvbnm'),
            'invite_type'          => $faker->numberBetween(1, 2),
            'last_login'           => $faker->dateTimeBetween('-10 days', 'now')->format('Y-m-d H:i:s'),
            'account'              => '',
        ];
    }
);

$factory->define(
    App\Models\Hospital::class,
    function (Faker\Generator $faker) {

        return [
            'name'              => $faker->company,
            'pic'               => $faker->imageUrl(),
            'introduction'      => $faker->text(),
            'type'              => $faker->numberBetween(1, 3),
            'sort'              => $faker->numberBetween(1, 30),
            'longitudelatitude' => $faker->longitude . ',' . $faker->latitude,
            'updated_by'        => $faker->numberBetween(1, 30),
            'province'          => 0,
            'city'              => 0,
            'county'            => 0,
        ];
    }
);

$factory->define(
    App\Models\Patient::class,
    function (Faker\Generator $faker) {

        $doctor = \App\User::find($faker->numberBetween(1, 30));

        return [
            'name'                 => $faker->name,
            'phone'                => $faker->phoneNumber,
            'password'             => bcrypt('secret'),
            'weight'               => $faker->randomFloat(1, 45, 100),
            'height'               => $faker->numberBetween(140, 200),
            'age'                  => $faker->numberBetween(40, 100),
            'bmi'                  => $faker->randomFloat(1, 15, 80),
            'hypertension'         => $faker->numberBetween(100, 160),
            'hypotension'          => $faker->numberBetween(60, 100),
            'heart_rate'           => $faker->numberBetween(60, 120),
            'sex'                  => $faker->boolean() ? 1 : 0,
            'birthday'             => $faker->date(),
            'province'             => 0,
            'city'                 => 0,
            'county'               => 0,
            'nation'               => 0,
            'invite_code'          => $faker->shuffleString('asdfghjklpoiuytrewqzxcvbnm'),
            'invite_original_code' => $faker->shuffleString('asdfghjklpoiuytrewqzxcvbnm'),
            'invite_type'          => $faker->numberBetween(1, 2),
            'diabetes_type'        => $faker->numberBetween(0, 4),
            'special_illness'      => $faker->numberBetween(0, 5),
            'is_gestation'         => $faker->numberBetween(0, 2),
            'hospital_id'          => $doctor->hospital->id,
            'user_doctor_id'       => $doctor->id,
            'illyears'             => $faker->date(),
            'treat_type'           => '饮食控制',
            'last_login'           => $faker->dateTimeBetween('-10 days', 'now')->format('Y-m-d H:i:s'),
            'account'              => '',
            'medical_number'       => $faker->randomNumber(7),
        ];
    }
);

$factory->define(
    App\Models\MedicalRecord::class,
    function (Faker\Generator $faker) {

        $doctor = \App\User::find($faker->numberBetween(1, 30));

        return [
            'patient_id'      => $faker->numberBetween(1, 30),
            'hospital_id'     => $doctor->hospital_id,
            'user_doctor_id'  => $doctor->id,
            'clinic_at'       => $faker->date(),
            'print_auxiliary' => '1,2,3,4',
            'medicine'        => '',
            'cn_medicine'     => '',
            'interval'        => $faker->text(),
            'visit_type'      => $faker->numberBetween(0, 1),
            'updated_by'      => $doctor->id,
        ];
    }
);

$factory->define(
    App\Models\MedicalComplaint::class,
    function (Faker\Generator $faker) {
        $symptom = [];

        $symptom['symptom'] = \App\Models\MedicalSymptom::find([1, 2, 33, 63], ['name']);

        $symptom['duration'] = [
            'year'  => $faker->numberBetween(3, 6),
            'month' => $faker->month,
            'day'   => $faker->dayOfMonth,
        ];

        $symptom['exacerbation'] = [
            'year'  => $faker->numberBetween(0, 3),
            'month' => $faker->month,
            'day'   => $faker->dayOfMonth,
        ];

        return [
            'record_id'          => $faker->numberBetween(1, 30),
            'symptom'            => json_encode([$symptom]),
            'weightchange'       => $faker->numberBetween(1, 3),
            'weightchangestatus' => $faker->numberBetween(0, 1),
            'remark'             => $faker->text(),
        ];
    }
);

$factory->define(
    \App\Models\Reservation\Reservation::class,
    function (Faker\Generator $faker) {

        $doctor = \App\User::onlyDoctors()->where(['hospital_id' => 3])->get()
                           ->pluck('id')->toArray();

        $patient = \App\Models\Patient::where(['hospital_id' => 3])
                                      ->has('medicalRecords')
                                      ->get()->pluck('id')->toArray();

        shuffle($doctor);
        shuffle($patient);
        $time = $faker->dateTimeBetween('now', '+10 days')->format('Y-m-d H:i:s');
        $time = \Carbon\Carbon::parse($time);

        return [
            'patient_id' => array_shift($patient),
            'doctor_id'  => array_shift($doctor),
            'am'         => ($time->addHour()->format('H') < 12) ? 1 : 0,
            'time'       => $time->format('Y-m-d'),
            'section'    => $time->addHour()->format('H'),
            'room'       => $faker->numberBetween(1, 15),
            'purpose'    => $faker->sentence,
            'remark'     => $faker->text(),
            'status'     => $faker->numberBetween(0, 3),
            'created_at' => \Carbon\Carbon::now(),
            'updated_at' => \Carbon\Carbon::now(),
        ];
    }
);

$factory->define(\App\Models\Reservation\Schedule::class, function (Faker\Generator $faker) {

    $start = \Carbon\Carbon::now()->startOfMonth();

    $end = \Carbon\Carbon::now()->endOfMonth();

    $time = $faker->dateTimeBetween($start, $end);

    $doctor = \App\User::onlyDoctors()->where(['hospital_id' => 3])->get()
                       ->pluck('id')->toArray();

    shuffle($doctor);

    return [
        'doctor_id' => array_shift($doctor),
        'time'      => $time->format('Y-m-d'),
        'room'      => $faker->numberBetween(1, 15),
        'workload'  => [
            1 => [
                [
                    'start'  => 8,
                    'end'    => 9,
                    'number' => $faker->numberBetween(1, 15),
                ],
                [
                    'start'  => 9,
                    'end'    => 10,
                    'number' => $faker->numberBetween(1, 15),
                ],
                [
                    'start'  => 10,
                    'end'    => 11,
                    'number' => $faker->numberBetween(1, 15),
                ],
                11 => [
                    'start'  => 11,
                    'end'    => 12,
                    'number' => $faker->numberBetween(1, 15),
                ],
            ],

            0 => [
                13 => [
                    'start'  => 13,
                    'end'    => 14,
                    'number' => $faker->numberBetween(1, 15),
                ],
                14 => [
                    'start'  => 14,
                    'end'    => 15,
                    'number' => $faker->numberBetween(1, 15),
                ],
                15 => [
                    'start'  => 15,
                    'end'    => 16,
                    'number' => $faker->numberBetween(1, 15),
                ],
                16 => [
                    'start'  => 16,
                    'end'    => 17,
                    'number' => $faker->numberBetween(1, 15),
                ],
                17 => [
                    'start'  => 17,
                    'end'    => 18,
                    'number' => $faker->numberBetween(1, 15),
                ],
                18 => [
                    'start'  => 18,
                    'end'    => 19,
                    'number' => $faker->numberBetween(1, 15),
                ],
            ],
        ],
    ];
});



//factory(App\Models\Reservation\Schedule::class, 200)->create();
//factory(App\Models\Reservation\Schedule::class, 11)->make();